<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="测评任务" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入测评任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被测评人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入被测评人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分人" prop="judgmentName">
        <el-input
          v-model="queryParams.judgmentName"
          placeholder="请输入评分人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分人角色" prop="judgmentRole">
        <el-input
          v-model="queryParams.judgmentRole"
          placeholder="请输入评分人角色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:mark:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="markList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="测评任务名称" align="center" prop="taskName" />
      <el-table-column label="被测评人名称" align="center" prop="userName" />
      <el-table-column label="评分人名称" align="center" prop="judgmentName" />
      <el-table-column label="评分人角色" align="center" prop="judgmentRole" />
      <el-table-column label="得分" align="center" prop="grade" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测评任务打分对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="测评任务" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入测评任务" />
        </el-form-item>
        <el-form-item label="测评任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入测评任务名称" />
        </el-form-item>
        <el-form-item label="被测评人ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入被测评人ID" />
        </el-form-item>
        <el-form-item label="被测评人名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入被测评人名称" />
        </el-form-item>
        <el-form-item label="评分人ID" prop="judgmentId">
          <el-input v-model="form.judgmentId" placeholder="请输入评分人ID" />
        </el-form-item>
        <el-form-item label="评分人名称" prop="judgmentName">
          <el-input v-model="form.judgmentName" placeholder="请输入评分人名称" />
        </el-form-item>
        <el-form-item label="评分人角色信息(A、B、C)" prop="judgmentRole">
          <el-input v-model="form.judgmentRole" placeholder="请输入评分人角色信息(A、B、C)" />
        </el-form-item>
        <el-form-item label="得分" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入得分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMark, getMark, delMark, addMark, updateMark } from "@/api/system/mark";

export default {
  name: "Mark",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测评任务打分表格数据
      markList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        taskName: null,
        userId: null,
        userName: null,
        judgmentId: null,
        judgmentName: null,
        judgmentRole: null,
        grade: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskId: [
          { required: true, message: "测评任务不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "测评任务名称不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "被测评人ID不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "被测评人名称不能为空", trigger: "blur" }
        ],
        judgmentId: [
          { required: true, message: "评分人ID不能为空", trigger: "blur" }
        ],
        judgmentName: [
          { required: true, message: "评分人名称不能为空", trigger: "blur" }
        ],
        judgmentRole: [
          { required: true, message: "评分人角色信息(A、B、C)不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "得分不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测评任务打分列表 */
    getList() {
      this.loading = true;
      listMark(this.queryParams).then(response => {
        this.markList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        taskName: null,
        userId: null,
        userName: null,
        judgmentId: null,
        judgmentName: null,
        judgmentRole: null,
        grade: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测评任务打分";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMark(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测评任务打分";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMark(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除测评任务打分编号为"' + ids + '"的数据项？').then(function() {
        return delMark(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mark/export', {
        ...this.queryParams
      }, `mark_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
